/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.lczyfz.wisdom.modules.platform.web;

import com.lczyfz.wisdom.common.config.Global;
import com.lczyfz.wisdom.common.persistence.Page;
import com.lczyfz.wisdom.common.utils.StringUtils;
import com.lczyfz.wisdom.common.web.BaseController;
import com.lczyfz.wisdom.modules.platform.entity.PUser;
import com.lczyfz.wisdom.modules.platform.service.PUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * puserController
 * @author css
 * @version 2019-09-26
 */
@Controller
@RequestMapping(value = "${adminPath}/puser/pUser")
public class PUserController extends BaseController {

	@Autowired
	private PUserService pUserService;
	
	@ModelAttribute
	public PUser get(@RequestParam(required=false) String id) {
		PUser entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = pUserService.get(id);
		}
		if (entity == null){
			entity = new PUser();
		}
		return entity;
	}
	
	@RequiresPermissions("puser:pUser:view")
	@RequestMapping(value = "list")
	public String list(PUser pUser, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PUser> page = pUserService.findPage(new Page<PUser>(request, response), pUser);
		model.addAttribute("page", page);
		return "modules/puser/pUserList";
	}

	@RequiresPermissions("puser:pUser:view")
	@RequestMapping(value = "form")
	public String form(PUser pUser, Model model) {
		model.addAttribute("pUser", pUser);
		return "modules/puser/pUserForm";
	}

	@RequiresPermissions("puser:pUser:edit")
	@RequestMapping(value = "save")
	public String save(PUser pUser, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, pUser)){
			return form(pUser, model);
		}
		pUserService.save(pUser);
		addMessage(redirectAttributes, "保存puser成功");
		return "redirect:"+ Global.getAdminPath()+"/puser/pUser/?repage";
	}
	
	@RequiresPermissions("puser:pUser:edit")
	@RequestMapping(value = "delete")
	public String delete(PUser pUser, RedirectAttributes redirectAttributes) {
		pUserService.delete(pUser);
		addMessage(redirectAttributes, "删除puser成功");
		return "redirect:"+Global.getAdminPath()+"/puser/pUser/?repage";
	}



}